//
//  LSUserAccountTools.swift
//  新浪微博
//
//  Created by 梁森 on 16/6/26.
//  Copyright © 2016年 梁森. All rights reserved.
//

import UIKit

class LSUserAccountTools: NSObject {
    
    let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userOauth.archiver")
    
    static let sharedTools:LSUserAccountTools = LSUserAccountTools()
    
    var userOauth:LSoAuthModel?
    
    var isLogIn:Bool{
        return accessToken != nil
    }
    
    var accessToken:String?{
        if userOauth?.access_token == nil{
            return nil
        }else{
            if userOauth?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
                return userOauth?.access_token
            }else{
                return nil
            }
        }
    }
    
    override init() {
        super.init()
        userOauth = getUserAccount()
    }
    
    //存储
    func saveUserAccount(userOauth:LSoAuthModel){
        self.userOauth = userOauth
        
        NSKeyedArchiver.archiveRootObject(userOauth, toFile: path)
    }
    func getUserAccount()->LSoAuthModel?{
        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? LSoAuthModel
        return result
    }
    
}
